package com.pet.pet.utlis;

import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class ValPuls {
    /**
     * 校验多个参数
     *
     * @param objects
     * @return
     */
    public Boolean isFlag(Object[] objects) {
        if (!isNullArrayObj(objects)) {
            for (Object object : objects) {
                return !isNullObj(object);
            }
            return true;
        }
        return false;
    }

    /**
     * 相等返回 true : false;
     *
     * @param val1
     * @param val2
     * @return
     */
    public final Boolean isEqual(Object val1, Object val2) {
        String str1 = isNullObj(typeOf(val1)) ? "" : typeOf(val1);
        String str2 = isNullObj(typeOf(val2)) ? "" : typeOf(val2);
        if (isNullString(str1) || isNullString(str2)){
            return false;
        }
        if (!str1.equals(BOOLEAN) && !str2.equals(BOOLEAN)) {
            return toStr(val1).equals(toStr(val2));
        }
        return (Boolean) val1 && (Boolean) val2;
    }

    private String typeOf(Object val) {
        if (!isNullObj(val)) {
            String str= val instanceof String ? STRING : val instanceof Integer ? INTEGER : val instanceof Boolean ? BOOLEAN : val instanceof Double ? DOUBLE : val instanceof Long ? LONG : null;
            return str;
        }
        return null;
    }


    public final boolean isNullString(String obj) {
        return isNullObj(obj) || obj.trim().equals("");
    }

    public final boolean isNullObj(Object obj) {
        return obj == null;
    }

    public final <T> boolean isNullListFast(List<T> list) {
        return isNullObj(list) || list.size() == 0;
    }

    public final boolean isNullArrayObj(Object[] objects) {
        return isNullObj(objects) || objects.length == 0;
    }

    private static final String STRING = "s";
    private static final String INTEGER = "i";
    private static final String DOUBLE = "d";
    private static final String BOOLEAN = "b";
    private static final String LONG = "l";


    private String toStr(Object val) {
        return String.valueOf(val);
    }
}
